﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace ListLimiter
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class ListLimiterReceiver : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

           bool limitEnabled = Common.GetLimitEnabled(properties.Web, properties.ListId);
           int limit = Common.GetLimit(properties.Web, properties.ListId);

           if (limitEnabled && properties.List.ItemCount >= limit)
           {
               properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
               properties.RedirectUrl = "/_layouts/ListLimiter/ListLimiterErrorPage.aspx";

               
           }
       }
    }
}
